package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/product/baseTrademark")
public class BaseTrademarkController {

    @Autowired
    private BaseTrademarkService baseTrademarkService;
    //  /admin/product/baseTrademark/{page}/{limit}
    @GetMapping("/{page}/{limit}")
    public Result getBaseTradeMarkList(@PathVariable Long page,
                                       @PathVariable Long limit){
        //  创建一个Page 对象
        Page<BaseTrademark> baseTrademarks = new Page<BaseTrademark>(page,limit);
        //  调用服务层方法.
        IPage iPage = this.baseTrademarkService.getBaseTradeMarkList(baseTrademarks);
        //  返回数据.
        return Result.ok(iPage);
    }

    //  /admin/product/baseTrademark/save
    @PostMapping("save")
    public Result saveBaseTradeMark(@RequestBody BaseTrademark baseTrademark){
        //  调用服务层方法.
        this.baseTrademarkService.save(baseTrademark);
        return Result.ok();
    }

    //  删除：
    //  /admin/product/baseTrademark/remove/{id}
    @DeleteMapping("/remove/{id}")
    public Result removeById(@PathVariable Long id){
        //  调用服务层方法.
        this.baseTrademarkService.removeById(id);
        return Result.ok();
    }

    //  根据品牌Id 获取到品牌对象
    @GetMapping("/get/{id}")
    public Result getById(@PathVariable Long id){
        //  调用服务层方法.
        BaseTrademark baseTrademark = this.baseTrademarkService.getById(id);
        //  返回数据.
        return Result.ok(baseTrademark);
    }

    //  保存修改的内容：
    @PutMapping("/update")
    public Result update(@RequestBody BaseTrademark baseTrademark){
        //  调用服务层方法.
        this.baseTrademarkService.updateById(baseTrademark);
        return Result.ok();
    }
}
